{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 字符串操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello,Python\n",
      "这\n",
      "是\n",
      "Python 量化投资\n",
      "It's a dog!\n",
      "It's a dog!\n",
      "hello boy\n",
      "hello boy\n",
      "hello boy \\n hello boy\n",
      "cbx\n",
      "cbx\n",
      "cbx\n",
      "\n",
      "caobinxin,changyang\n",
      "bin\n"
     ]
    }
   ],
   "source": [
    "# 双引号\n",
    "c1 = \"Hello,Python\"\n",
    "print(c1)\n",
    "\n",
    "c2 = \"\"\"这\n",
    "是\n",
    "Python 量化投资\"\"\"\n",
    "print(c2)\n",
    "\n",
    "c3 = \"It's a dog!\"\n",
    "print(c3)\n",
    "\n",
    "c4 = 'It\\'s a dog!'\n",
    "print(c4)\n",
    "\n",
    "print( \"hello boy\\nhello boy\")\n",
    "print( r\"hello boy \\n hello boy\") # 通过在字符串前面加上 r 表示，\\ 不是转译字符\n",
    "\n",
    "print( \"cbx\\n\"*3) # 重复输出字符串\n",
    "\n",
    "c5 = \"caobinxin,changyang\"\n",
    "print(c5[:])\n",
    "print(c5[3:6])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.列表 List"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '小华', '小李', '小娟', '小于', 3]\n",
      "['小明', '小华', '小李']\n",
      "3\n",
      "小李\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "students = [\"小明\", \"小华\", \"小李\", \"小娟\", \"小于\", 3]\n",
    "\n",
    "print(students)\n",
    "\n",
    "print(students[:3])\n",
    "print(students[-1])\n",
    "print(students[2])\n",
    "print(len(students))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 列表可以存放不同类型的数据，是最常用的python 数据类型。\n",
    "- 和字符串不同，列表元素支持改写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '小华', '小李', '小娟', '小于', 3]\n",
      "['小明', '小华', '小李', '小娟', '小于', 5]\n"
     ]
    }
   ],
   "source": [
    "# 列表元素支持改写\n",
    "students = [\"小明\", \"小华\", \"小李\", \"小娟\", \"小于\", 3]\n",
    "\n",
    "print(students)\n",
    "\n",
    "students[-1] = 5\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 元组 Tuple"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 在python 中和数组类似的还有元组，元组中的元素也进行索引计算。\n",
    "- 列表和元组的区别在于：\n",
    "    - 列表中的元素的值可以修改，而元组中的元素的值不可以修改，只可以读取。\n",
    "    - 列表的符号是 [] ，而元组的符号是()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('小明', '小花', '小李', '小曹', 4)\n",
      "小花\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-25-b8325c96a693>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstudents\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mstudents\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"小陈\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstudents\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "students = (\"小明\", \"小花\", \"小李\", \"小曹\", 4)\n",
    "print(students)\n",
    "print(students[1])\n",
    "\n",
    "students[1] = \"小陈\"\n",
    "print(students[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 元组比列表更加安全，因为不能修改，所以可以安全地保存一个引用，而不用担心里面的内容被其他程序修改，从而导致逻辑错误"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 集合 Set"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 在python 中集合主要有两个功能：\n",
    "    - 一个功能是进行集合操作\n",
    "    - 另一个功能是消除重复元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 4, 4, 4]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2, 3, 4, 4, 4, 4]\n",
    "print(a)\n",
    "set(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'c', 'g', 'm', 'a', 'b', 's', 'n'}\n",
      "{'c', 'm', 'f', 'd', 'l'}\n",
      "{'c', 'm'}\n",
      "{'c', 'g', 'm', 'a', 'b', 'f', 's', 'd', 'n', 'l'}\n",
      "{'d', 'f', 'l'}\n",
      "{'c', 'g', 'm', 's', 'a', 'n', 'b'}\n"
     ]
    }
   ],
   "source": [
    "a = set(\"abcnmaaaaggsng\")\n",
    "print(a)\n",
    "b = set(\"cdfml\")\n",
    "print(b)\n",
    "\n",
    "# 交集\n",
    "x=a&b\n",
    "print(x)\n",
    "\n",
    "# 并集\n",
    "y=a|b\n",
    "print(y)\n",
    "\n",
    "# 差集\n",
    "z = b - a\n",
    "print(z)\n",
    "\n",
    "# 去除重复元素\n",
    "new = set(a)\n",
    "print(new)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.字典 Dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 在 python 中的字典也叫做 关联数组，可以理解为列表的升级版，用 大括号{} 括起来。\n",
    "- 字典的每个键值 (key => value)对用冒号分割，每个对之间用逗号分割，整个字典包括在花括号中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "山东\n",
      "王小川\n",
      "游泳\n",
      "dict_keys(['姓名', '籍贯', '爱好'])\n",
      "3\n",
      "{'姓名': '王小川', '籍贯': '山东', '爱好': '游泳'}\n",
      "<class 'dict'>\n",
      "{'姓名': '王小川', '籍贯': '山东', '爱好': '游泳'}\n",
      "haha\n",
      "{'姓名': '王小川', '籍贯': '山东', '爱好': '游泳'}\n",
      "...........\n",
      "dict_items([('姓名', '王小川'), ('籍贯', '山东'), ('爱好', '游泳')])\n",
      "dict_keys(['姓名', '籍贯', '爱好'])\n",
      "陈阳\n",
      "{'姓名': '王小川', '籍贯': '山东', '爱好': '游泳', '配偶': '陈阳'}\n",
      "{'姓名': '王小川', '籍贯': '山东', '爱好': '游泳', '配偶': '陈阳', '父亲': '曹学明', '母亲': '杜娟芳'}\n",
      "dict_values(['王小川', '山东', '游泳', '陈阳', '曹学明', '杜娟芳'])\n"
     ]
    }
   ],
   "source": [
    "# 字典\n",
    "k = {\"姓名\":\"王小川\", \"籍贯\":\"山东\"}\n",
    "print(k[\"籍贯\"])\n",
    "\n",
    "k[\"爱好\"]=\"游泳\"\n",
    "print(k[\"姓名\"])\n",
    "print(k[\"爱好\"])\n",
    "\n",
    "print(k.keys()) # 可以取到所有的key\n",
    "\n",
    "print(len(k))\n",
    "print(str(k))\n",
    "print(type(k))\n",
    "\n",
    "# k.clear() # 删除字典内的所有元素\n",
    "# print(k)\n",
    "\n",
    "k1 = k.copy()\n",
    "print(k1)\n",
    "\n",
    "print(k.get(\"配偶\", \"haha\"))\n",
    "print(k)\n",
    "print(\"...........\")\n",
    "print(k.items()) # 以列表形式返回 可遍历的（键，值） 元组 数组\n",
    "\n",
    "print(k.keys())# 以列表的形式 返回一个字典的所有键\n",
    "\n",
    "print(k.setdefault(\"配偶\", \"陈阳\")) # 和 get() 类似， 但如果键不存在于字典中，则将会添加键并将值设为 default\n",
    "print(k)\n",
    "\n",
    "new_k = {\"父亲\":\"曹学明\", \"母亲\":\"杜娟芳\"}\n",
    "k.update(new_k) # 把字典 new_k 中的键值对更新到 k 中\n",
    "print(k)\n",
    "\n",
    "print(k.values()) # 以列表的形式返回 字典中的所有值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. 关键字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n"
     ]
    }
   ],
   "source": [
    "import keyword\n",
    "\n",
    "print(keyword.kwlist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
